package cn.edu.sdjzu.xg.eagle.service;

import cn.edu.sdjzu.xg.eagle.dao.UserDaoImpl;
import cn.edu.sdjzu.xg.eagle.dao.UserDaoInterface;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.util.JdbcPoolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

public final class UserServiceImpl implements UserServiceInterface {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    private final UserDaoInterface userDao =
            DaoFactory.getDao(UserDaoImpl.class, "UserDaoImpl");

    @Override
    public void login(User userToLogin) throws SQLException {
        try (Connection connection = JdbcPoolUtil.getConn()) {
            userDao.login(connection, userToLogin);
        } catch (SQLException e) {
            logger.error("用户登录时数据库操作异常，用户名: {}", userToLogin.getUsername(), e);
            throw e;
        }
    }

    @Override
    public User findById(Long id) throws SQLException {
        try (Connection connection = JdbcPoolUtil.getConn()) {
            return userDao.findById(connection, id);
        } catch (SQLException e) {
//            logger.error("用户登录时数据库操作异常，用户名: {}", userToLogin.getUsername(), e);
            throw e;
        }

    }
}
